/*
* JS封装 首页,列表页,详情页等html
* 
*/

//====================全局变量=========================
var modelName;
var pageTotal;
var pageSize = 15;


//================================ 首页 start ================================

function toHome() {
	//加载首页数据
	loadHomeData('getNewsList', 6);  //新闻中心
	loadHomeData('getHospitalDesc');  //医院简介图文详情
	loadHomeData('getExpertDescList', 15);  //专家介绍
	loadHomeData('getModelInfoList', 7, 'SRT'); //科研教学
	loadHomeData('getModelInfoList', 7, 'HE');
	loadHomeData('getModelInfoList', 7, 'NG');
	loadHomeData('getModelInfoList', 7, 'PBG');
	loadHomeData('getModelInfoList', 7, 'MIP');
	loadHomeData('getModelInfoList', 7, 'PA');
	loadHomeData('getModelInfoList', 7, 'PR');
	loadHomeData('getModelInfoList', 7, 'KCM');
	loadHomeData('getFriendlyLinkList');  //友情链接
	
	$('#rightContent').empty();
	
	var str = '<div>'+
							'<div class="rightblock yy-news">'+
								'<img class="leftfloat newsPic" id="newsPic"/>'+
								'<div class="leftfloat newsText" id="newsText"></div>'+
								'<div class="rightfloat newsList">'+
									'<div class="parent rightblock-title title">'+
										'<span class="leftfloat">新闻中心</span>'+
										'<a class="rightfloat" onclick="toList('+"'"+"/listPage/getNewsPage"+"', " + "''," + "''," + '28, 1)">更多</a>'+
									'</div>'+
									'<ul class="layui-icon-loading" id="newsList" style="height:236px;"></ul>'+
								'</div>'+
							'</div>'+
							'<div class="rightblock yy-specialist">'+
								'<div class="rightblock-title">'+
									'<span>专家介绍</span>'+
								'</div>'+
								'<div class="specialist layui-carousel" id="slidermain">'+
									'<ul carousel-item id="sliderContent" class="specialist-list layui-icon-loading-1"></ul>'+
								'</div>'+
							'</div>'+
							'<div class="parent rightblock yy-listblock">'+
								'<div class="parent rightblock-title">'+
									'<span class="leftfloat">科研教学</span>'+
									'<a class="leftfloat" style="margin-left: 330px;" onclick="toList('+"'"+"/listPage/getModelInfoPage"+"', " + "''," + "'SRT'" + ','+pageSize+', 1)">更多</a>'+
									 '<span class="leftfloat">科普知识</span>'+  //合并原来的中医知识和健康教育
									'<a class="rightfloat" style="margin-left: 330px;" onclick="toList('+"'"+"/listPage/getModelInfoPage"+"', " + "''," + "'HE'" + ','+pageSize+', 1)">更多</a>'+
								'</div>'+
								'<ul class="leftfloat rightblock-list-left layui-icon-loading" id="research"></ul>'+
								'<ul class="rightfloat rightblock-list-right layui-icon-loading" id="health"></ul>'+
							'</div>'+
							'<div class="parent rightblock yy-listblock">'+
								'<div class="parent rightblock-title">'+
									'<span class="leftfloat">护理园地</span>'+
									'<a class="leftfloat" style="margin-left: 330px;" onclick="toList('+"'"+"/listPage/getModelInfoPage"+"', " + "''," + "'NG'" + ','+pageSize+', 1)">更多</a>'+
									'<span class="leftfloat">党建园地</span>'+
									'<a class="rightfloat" style="margin-left: 330px;" onclick="toList('+"'"+"/listPage/getModelInfoPage"+"', " + "''," + "'PBG'" + ','+pageSize+', 1)">更多</a>'+
								'</div>'+
								'<ul class="leftfloat rightblock-list-left layui-icon-loading" id="tcm"></ul>'+
								'<ul class="rightfloat rightblock-list-right layui-icon-loading" id="nurse"></ul>'+
							'</div>'+
							'<div class="parent rightblock yy-listblock">'+
								'<div class="parent rightblock-title">'+
									'<span class="leftfloat">医保政策</span>'+
									'<a class="leftfloat" style="margin-left: 330px;" onclick="toList('+"'"+"/listPage/getModelInfoPage"+"', " + "''," + "'MIP'" + ','+pageSize+', 1)">更多</a>'+
									'<span class="leftfloat">药事管理</span>'+
									'<a class="rightfloat" style="margin-left: 330px;" onclick="toList('+"'"+"/listPage/getModelInfoPage"+"', " + "''," + "'PA'" + ','+pageSize+', 1)">更多</a>'+
								'</div>'+
								'<ul class="leftfloat rightblock-list-left layui-icon-loading" id="partyBuild"></ul>'+
								'<ul class="rightfloat rightblock-list-right layui-icon-loading" id="medicalInsurance"></ul>'+
							'</div>'+
							'<div class="parent rightblock yy-listblock">'+
								'<div class="parent rightblock-title">'+
									'<span class="leftfloat">政策法规</span>'+
									'<a class="leftfloat" style="margin-left: 330px;" onclick="toList('+"'"+"/listPage/getModelInfoPage"+"', " + "''," + "'PR'" + ','+pageSize+', 1)">更多</a>'+
									'<span class="leftfloat">对口帮扶</span>'+
									'<a class="rightfloat" style="margin-left: 330px;" onclick="toList('+"'"+"/listPage/getModelInfoPage"+"', " + "''," + "'KCM'" + ','+pageSize+', 1)">更多</a>'+
								'</div>'+
								'<ul class="leftfloat rightblock-list-left layui-icon-loading" id="pharmaceutical"></ul>'+
								'<ul class="rightfloat rightblock-list-right layui-icon-loading" id="policy"></ul>'+
							'</div>'+
							'<div class="rightblock yy-other-nav">'+
								'<div class="rightblock-title">'+
									'<span>其他网站导航</span>'+
								'</div>'+
								'<div class="other-nav-list" id="friendlyLink"></div>'+
							'</div>'+
						'</div>';
						
	$('#rightContent').append(str);
}

function loadHomeData(requestUrl, len, modelName){
	$.ajax({
		type:"get",
		url: server+pageName+requestUrl,
		dataType:"json",
		async:true,
		data: {length: len, modelName: modelName},
		success: function(data){
			if(data.resultCode == 0){
				var getData = data.resultData.dataList;

				if(requestUrl == 'getNewsList'){ //新闻
					showNewsList(getData);
				}
				
				if(requestUrl == 'getHospitalDesc'){  //查看详情
					showNewsText(data.resultData.pictures['url'],data.resultData.content, data.resultData.value);
				}
				
				if(requestUrl == 'getExpertDescList'){  //专家列表
					showExpertDescList(getData, server);
				}
				
				if(requestUrl == 'getFriendlyLinkList'){  //友情链接
					showFriendlyLink(data.resultData);
				}
	
				if(requestUrl == 'getModelInfoList'){  //科研教学等栏目
					showModelList(getData, modelName);
				}
			}
		},
		error: function(err){
			console.log('请求出错')
		}
	});
}

//新闻列表
function showNewsList(arr){
	arr.map(function(item, index){
		var pageId = item.value;
		var $a = $('<a onclick="toDetails('+"'"+pageId+"'"+')">'+item.title+'</a>');
		var $li = $('<li></li>');
		$li.append($a);
		$('#newsList').append($li);
	})
	$('#newsList').removeClass('layui-icon-loading');
}

//新闻详情
function showNewsText(picUrl,content, alink){
	$('#newsPic').attr('src', picUrl);
	if(content.length > 164){
		content = content.substr(0, 164) + '…';
	}
	var pageId = alink;
	$('#newsText').html(content+'&nbsp;<a onclick="toDetails('+"'"+pageId+"'"+')">[查看详情]</a>');
}

//专家介绍
function showExpertDescList(arr, server){
	var parentArr = [],
			childArr = [];

	arr.map(function(item, index){
		var $a = $('<a onclick="toDetails('+"'"+item.value+"'"+')"><img src="'+item.picUrl+'"><span class="name">'+item.name+'</span><span class="job">'+item.rank+'</span></a>');
		childArr.push($a);
	})
	
	parentArr = chunk(childArr, 5); //按一定长度将数组childArr分割，得到新的数组
	parentArr.map(function(item, index){
		var $li = $('<li></li>');
		$li.append(item);
		$('#sliderContent').append($li);
	})
	$('#sliderContent').removeClass('layui-icon-loading-1');

	
	
	//轮播图
	layui.use('carousel', function(){
		var carousel = layui.carousel;
		//建造实例
		carousel.render({
			elem: '#slidermain'
			,width: '911' //设置容器宽度
			,height: '265'
			,autoplay: true
			//,arrow: 'always' //始终显示箭头
			//,anim: 'updown' //切换动画方式
		});
	});
}

function chunk(array, size) {
	//获取数组的长度，如果你传入的不是数组，那么获取到的就是undefined
	var length = array.length;
	//判断不是数组，或者size没有设置，size小于1，就返回空数组
	if (!length || !size || size < 1) {
		return []
	}
	//核心部分
	var index = 0; //用来表示切割元素的范围start
	var resIndex = 0; //用来递增表示输出数组的下标 

	//根据length和size算出输出数组的长度，并且创建它。
	var result = new Array(Math.ceil(length / size));
	//进行循环
	while (index < length) {
		//循环过程中设置result[0]和result[1]的值。该值根据array.slice切割得到。
		result[resIndex++] = array.slice(index, (index += size));
	}
	//输出新数组
	return result;
}

//科研教学
function showModelList(arr, modelName){
	console.log(arr);
	arr.map(function(item, index){
		var pageId = item.value;
		var $a = '<a onclick="toDetails('+"'"+pageId+"'"+')">'+item.title+'</a>';
		var $li = $('<li></li>');
		$li.append($a); 
		
		if(modelName == 'SRT'){
			$('#research').append($li);
			$('#research').removeClass('layui-icon-loading');
		}
		
		if(modelName == 'HE'){
			$('#health').append($li);
			$('#health').removeClass('layui-icon-loading');
		}
		
		if(modelName == 'KCM'){
			$('#policy').append($li);
			$('#policy').removeClass('layui-icon-loading');
		}
		
		if(modelName == 'NG'){
			$('#tcm').append($li);
			$('#tcm').removeClass('layui-icon-loading');
		}
		
		if(modelName == 'PBG'){
			$('#nurse').append($li);
			$('#nurse').removeClass('layui-icon-loading');
		}
		
		if(modelName == 'MIP'){
			$('#partyBuild').append($li);
			$('#partyBuild').removeClass('layui-icon-loading');
		}
		
		if(modelName == 'PA'){
			$('#medicalInsurance').append($li);
			$('#medicalInsurance').removeClass('layui-icon-loading');
		}
		
		if(modelName == 'PR'){
			$('#pharmaceutical').append($li);
			$('#pharmaceutical').removeClass('layui-icon-loading');
		}
	})
}

//友情链接
function showFriendlyLink(arr){
	arr.map(function(item, index){
		var $a = $('<a href="'+item.linkUrl+'" target="_blank"><img src="'+item.picUrl+'"/></a>');
		$('#friendlyLink').append($a);
	})
}


//=====================点击更多，获取各个模块列表====================

// =================================列表页 start ===================================
/*
*toList参数：
* url：列表页的url
* id：列表页的id
* modelName: 模块名
* pageSize：每页显示条数
* pageNo：当前页码
*/

function toList(url, id, modelName, pageSize, pageNo) {
	var id = id == undefined || id == '' ? '' : id,
	    modelName = modelName == undefined || modelName == '' ? '' : modelName,
			pageSize = pageSize == undefined || pageSize == '' ? 15 : pageSize,
			pageNo = pageNo == undefined || pageNo == '' ? 1 : pageNo;
	
	$('#rightContent').empty();
	var str = '<div class="detail-list-right">'+
							'<ul class="news-list-box layui-icon-loading" id="modelList"></ul>'+
						'</div>';
						
	$('#rightContent').append(str);
	$('html,body').scrollTop(0);
	
	if(url == '/listPage/getNoticePage'){  //医院公告
		getNoticeList(url, pageSize, pageNo);
	}
	
	if(url == '/listPage/getNewsPage'){  //新闻中心
		getNewsList(url, pageSize, pageNo);
	}
	
	if(url == '/listPage/getAllDepartmentExpertList'){  //专家介绍
		getExpertList(url, pageSize, pageNo);
	}
	
	if(url == '/listPage/getModelInfoPage'){
		showListPages(modelName, pageSize, pageNo);  //科研教育等栏目
	}
	
	$('#modelList').removeClass('layui-icon-loading');
	
}

//获取 医院公告更多列表。参数url、每页显示条数、当前页码
function getNoticeList(url, pageSize, pageNo){
	$('.nav > ul li').removeClass('current').eq(0).addClass('current');
	$.ajax({
		type:"get",
		url: server+url+'?length='+pageSize+'&pageNo='+pageNo,
		async:true,
		dataType: "json",
		success: function(data){
			if(data.resultCode == 0){
				var listData = data.resultData.dataList;
				pageTotal = data.resultData.total;
				
				for (var i=0; i<listData.length; i++) {
					var pageId = listData[i].value;
					var $a = $('<a onclick="toDetails('+"'"+pageId+"'"+')">'+listData[i].title+'</a>');
					var $span = $('<span>'+listData[i].createTime+'</span>');
					var $li = $('<li></li>');
					$li.append($a, $span);
					$("#modelList").append($li);
				}
				$("#modelList").before('<h1 class="download-title">医院公告</h1>');
				if(listData.length){
					$("#modelList").after('<div id="page2" style="text-align: center;margin-top: 32px;"></div>');
					changePage(url, '', pageTotal);
				}
				
			}
		},
		error: function(err){
			console.log('请求出错');
		}
	});
	
} 

//获取 新闻更多列表 。参数url、每页显示条数、当前页码
function getNewsList(url, pageSize, pageNo){
	$.ajax({
		type:"get",
		url: server+url+'?length='+pageSize+'&pageNo='+pageNo,
		async:true,
		dataType: "json",
		success: function(data){
			if(data.resultCode == 0){
				var listData = data.resultData.dataList;
				pageTotal = data.resultData.total;
				
				for (var i=0; i<listData.length; i++) {
					var pageId = listData[i].value;
					var $a = $('<a onclick="toDetails('+"'"+pageId+"'"+')">'+listData[i].title+'</a>');
					var $span = $('<span>'+listData[i].createTime+'</span>');
					var $li = $('<li></li>');
					$li.append($a, $span);
					$('#modelList').append($li);
				}
				$("#modelList").before('<h1 class="download-title">新闻中心</h1>');
				if(listData.length){
					$("#modelList").after('<div id="page2" style="text-align: center;margin-top: 32px;"></div>');
					changePage(url, '', pageTotal);
				}
			}
		},
		error: function(err){
			console.log('请求出错');
		}
	});
}

//专家列表
function getExpertList(url, pageSize, pageNo){
	$.ajax({
		type:"get",
		url: server+url,
		//url: server+url+'?length='+pageSize+'&pageNo='+pageNo,
		async:true,
		dataType: "json",
		success: function(data){
			if(data.resultCode == 0){
				var listData = data.resultData;
				listLength = data.resultData.total;
				totalPage = Math.ceil(listLength/28);
				console.log(listData);
				
				for (var i=0; i<listData.length; i++) {
					var $li = $('<li></li>');
					//var pageId = listData[i].value;
					var $span = $('<span style="color: #333;">【'+listData[i].name+'】</span>');
					var $div = $('<div style="float: left;"></div>');
					$li.append($span, $div);
					
					if(listData[i].expertList.length){
						for(var j=0; j<listData[i].expertList.length; j++){
							var $a = $('<a style="display: inline;max-width: initial;margin: 0 10px;" onclick="toDetails('+"'"+listData[i].expertList[j].value+"'"+')">'+listData[i].expertList[j].name+'</a>');
							$div.append($a);
						}
					}

					$('#modelList').append($li);
				}
				$("#modelList").before('<h1 class="download-title">专家介绍</h1>');
			}
		},
		error: function(err){
			console.log('请求出错');
		}
	});
}


//获取科研教学等栏目 更多列表
function showListPages(modelName, pageSize, pageNo){
	$.ajax({
		type:"get",
		url: server+listPage+'?length='+pageSize+'&modelName='+modelName+'&pageNo='+pageNo,
		async:true,
		dataType: "json",
		success: function(data){
			if(data.resultCode == 0){
				pageTotal = data.resultData.total;
				var listData = data.resultData.dataList;
				for (var i=0; i<listData.length; i++) {
					var pageId = listData[i].value;
					var $a = $('<a onclick="toDetails('+"'"+pageId+"'"+')">'+listData[i].title+'</a>');
					var $span = $('<span>'+listData[i].createTime+'</span>');
					var $li = $('<li></li>');
					$li.append($a, $span);
					$('#modelList').append($li);
				}
				if(listData.length){
					$("#modelList").after('<div id="page2" style="text-align: center;margin-top: 32px;"></div>');
					changePage('', modelName, pageTotal);
				}
				
				if(modelName == 'SRT'|| modelName == 'srt'){
					$("#modelList").before('<h1 class="download-title">科研教学</h1>');
				}
				if(modelName == 'HE' || modelName == 'he'){
					$("#modelList").before('<h1 class="download-title">科普知识</h1>');
				}
				
				if(modelName == 'NG'|| modelName == 'ng'){
					$("#modelList").before('<h1 class="download-title">护理园地</h1>');
				}
				if(modelName == 'PBG' || modelName == 'pbg'){
					$("#modelList").before('<h1 class="download-title">党建园地</h1>');
				}
				if(modelName == 'MIP' || modelName == 'mip'){
					$("#modelList").before('<h1 class="download-title">医保政策</h1>');
				}
				if(modelName == 'PA'|| modelName == 'pa'){
					$("#modelList").before('<h1 class="download-title">药事管理</h1>');
				}
				if(modelName == 'PR' || modelName == 'pr'){
					$("#modelList").before('<h1 class="download-title">政策法规</h1>');
				}
				if(modelName == 'KCM' || modelName == 'kcm'){
					$("#modelList").before('<h1 class="download-title">对口帮扶</h1>');
				}
				if(modelName == 'lzjxx'){
					$("#modelList").before('<h1 class="download-title">资金信息</h1>');
				}
				if(modelName == 'lxxgk'){
					$("#modelList").before('<h1 class="download-title">信息公开</h1>');
				}
				if(modelName == 'lghjh'){
					$("#modelList").before('<h1 class="download-title">规划计划</h1>');
				}
				if(modelName == 'lxzsp'){
					$("#modelList").before('<h1 class="download-title">行政审批</h1>');
				}
				if(modelName == 'lywgz'){
					$("#modelList").before('<h1 class="download-title">业务工作</h1>');
				}
				
			}
		},
		error: function(err){
			console.log('请求出错');
		}
	});
	
}

//分页
function changePage(url, modelName, pageTotal) {		
	layui.use('laypage', function(){
		var laypage = layui.laypage;
		
		//执行一个laypage实例
		laypage.render({
			elem: 'page2'
			,limit: 15   //每页显示的条数
			,count: pageTotal  //数据总数，从服务端得到
			,theme: '#96403f' 
			,jump: function(obj, first){
				//obj包含了当前分页的所有参数，比如：
				console.log('当前页：', obj.curr); //得到当前页，以便向服务端请求对应页的数据。
				console.log('每页显示的条数：', obj.limit); //得到每页显示的条数
				var pageSize = 15;
				var pageNo = obj.curr;
				//首次不执行
				if(!first){
					if(url == '/listPage/getNewsPage'){
						pageUrl = server+url+'?length='+pageSize+'&pageNo='+pageNo;  //新闻中心
						
					}else if(url == '/listPage/getNoticePage'){
						pageUrl = server+url+'?length='+pageSize+'&pageNo='+pageNo;  //医院公告
						
					}else if(url == '/expand/getDownLoadFileList'){
						pageUrl = server+url+'?length='+pageSize+'&pageNo='+pageNo;  //下载专区
						
					}else{
						pageUrl = server+listPage+'?length='+pageSize+'&modelName='+modelName+'&pageNo='+pageNo;  //科研教学等栏目url
					}
					
					$.ajax({
						type:"get",
						url: pageUrl,
						async:true,
						dataType: "json",
						success: function(data){
							$('#modelList').empty();
							$('#downloadList').empty();
							if(data.resultCode == 0){
								var listData = data.resultData.dataList;
								// for (var i=0; i<listData.length; i++) {
								// 	var pageId = listData[i].value;
								// 	var $a = $('<a onclick="toDetails('+"'"+pageId+"'"+')">'+listData[i].title+'</a>');
								// 	var $span = $('<span>'+listData[i].createTime+'</span>');
								// 	var $li = $('<li></li>');
								// 	$li.append($a, $span);
								// 	$('#modelList').append($li);
								// }
								if(url == '/expand/getDownLoadFileList'){
									setDownLoadList(listData);
								}else{
									setList(listData);
								}
								
								$('html,body').scrollTop(0);
							}
						},
						error: function(err){
							console.log('请求出错');
						}
					});
				}
			}
		});
	});
}

function setList(arr) {
	for (var i=0; i<arr.length; i++) {
		var pageId = arr[i].value;
		var $a = $('<a onclick="toDetails('+"'"+pageId+"'"+')">'+arr[i].title+'</a>');
		var $span = $('<span>'+arr[i].createTime+'</span>');
		var $li = $('<li></li>');
		$li.append($a, $span);
		$('#modelList').append($li);
	}
}

function setDownLoadList(arr) {
	for(var i=0; i<arr.length; i++){
		var str = '<li style="padding-left:8px; padding-right:14px;">'+
								'<a href="javascript:;" onClick="downloadFile('+"'"+arr[i].downloadUrl+"'"+')">'+arr[i].filename+'</a>'+
								'<span>'+arr[i].cteatedTime+'</span>'+
							'</li>';
		$('#downloadList').append(str);
	}
}


//========================================= 详情页 start ====================================

function toDetails(pageId, isBack) {
	var isBack = isBack == undefined ? true : isBack;  //是否有返回按钮
	$('#rightContent').empty();
	
	var str = '<div class="detail-list-right">'+
							'<div id="goback" style="cursor: pointer;">&lt;&lt;返回</div>'+
							'<div class="article">'+
							  '<h1 class="article-title" id="articleTitle"></h1>'+
								'<div class="article-info">'+
								  '<span>海口市中医医院:<font id="address"></font></span>'+
									'<span>文字来源:<font id="articleAuthor"></font></span>'+
									'<span>时间:<font id="articleTime"></font></span>'+
								'</div>'+
								'<div class="article-content layui-icon-loading" id="articleContent"></div>'+
							'</div>'+
						'</div>';

	$('#rightContent').append(str);
	$('html,body').scrollTop(0);

	gotoDetailsPage(pageId, isBack);
	$('#articleContent').removeClass('layui-icon-loading');

}

//获取详情页  toList(url, id, modelName, pageSize, pageNo)
function gotoDetailsPage(pageId, isBack){
  $.ajax({
		type:"get",
		url: server + '/detailsPage/getPage?id='+pageId,
		async:true,
		dataType: "json",
		success: function(data){
			var data = data.resultData;
			console.log(data);
			$('#articleTitle').html(data.title);
			$('#address').html(data.hospitalWebUrl);
			$('#articleAuthor').html(data.author);
			$('#articleTime').html(data.createTime);
			$('#articleContent').html(data.content);
			
			//判断是否是专家详情页，是否有图片
			if(data.picUrl){
				var str = '<div class="leftfloat exportPic">'+
										'<img src="'+data.picUrl+'">'+
										'<div>'+data.name+'</div>'+
									'</div>';
				$('#articleContent').before(str);
			}
			
			//判断是否有专家列表，判断是否科室列表的详情页，若是，就在详情页最后边添加相对应科室的专家
			if(data.expertList){
				var expertors = $('<div class="expertors"><h2 style="line-height: 2;text-align: center; margin-bottom: 10px;">'+data.title+'专家</h2></div>');
				var $ul = $('<ul></ul>');
				expertors.append($ul);
				for (var i=0; i<data.expertList.length; i++) {
					var $li = $('<li onclick="toDetails('+"'"+data.expertList[i].value+"'"+', false)"></li>');
					var $img = $('<img src="'+data.expertList[i].picUrl+'">');
					var $div1 = $('<div class="div1">'+data.expertList[i].name+'</div>');
					var $div2 = $('<div class="div2">'+data.expertList[i].rank+'</div>');
					$li.append($img, $div1, $div2);
					$ul.append($li);
				}
				$('#articleContent').append(expertors);
			}
			
			
			//点击返回
			if(data.modelListUrl != '' && data.modelListUrl != undefined || data.modelCode != '' && data.modelCode != undefined){
				console.log('返回页面url:'+data.modelListUrl);
				console.log('返回页面模块名:'+data.modelCode);
				if(isBack == true){  //如果有返回按钮，就执行以下
					$('#goback').show();
					$('#goback').click(function(){

						if(data.modelListUrl == '/listPage/getExpertPage'){  //如果url为专家列表的url，返回专家列表页
							toList('/listPage/getAllDepartmentExpertList', '', data.modelCode, '', '');
						}else{
							toList(data.modelListUrl, '', data.modelCode, '', '');
						}
						
					})
				}
			}
			
		},
		error: function(err){
			console.log('请求出错');
		}
	}); 
}


//==========================邮箱表单============================

function toForm(){
	$('#main').hide();
	$('#form').empty();

	var str =	'<div class="layui-form-wrap">'+
							'<form class="layui-form" id="formEmail">'+
								'<div class="layui-form-item">'+
									'<label class="layui-form-label">标题</label>'+
									'<div class="layui-input-block">'+
										'<input type="text" name="title" required lay-verify="required" autocomplete="off" class="layui-input">'+
									'</div>'+
								'</div>'+
								'<div class="layui-form-item">'+
									'<label class="layui-form-label">姓名</label>'+
									'<div class="layui-input-block">'+
										'<input type="text" name="senderName" required  lay-verify="required" autocomplete="off" class="layui-input">'+
									'</div>'+
								'</div>'+
								'<div class="layui-form-item">'+
									'<label class="layui-form-label">联系电话</label>'+
									'<div class="layui-input-block">'+
										'<input type="text" name="senderPhone" phone lay-verify="phone" autocomplete="off" class="layui-input">'+
									'</div>'+
								'</div>'+
								'<div class="layui-form-item">'+
									'<label class="layui-form-label">电子邮箱</label>'+
									'<div class="layui-input-block">'+
										'<input type="text" name="senderEmail" email autocomplete="off" class="layui-input">'+
									'</div>'+
								'</div>'+
								'<div class="layui-form-item layui-form-text">'+
									'<label class="layui-form-label">建议或意见</label>'+
									'<div class="layui-input-block">'+
										'<textarea name="desc" id="desc" required  lay-verify="required" placeholder="请输入内容" class="layui-textarea"></textarea>'+
									'</div>'+
								'</div>'+
								'<div class="layui-form-item">'+
									'<div class="layui-input-block">'+
										'<button class="layui-btn" id="sendEmail" lay-submit lay-filter="sendEmail" style="background-color: #1890ff;">提交</button>'+
										'<button type="reset" class="layui-btn layui-btn-primary">重置</button>'+
									'</div>'+
								'</div>'+
							'</form>'+
						'</div>';
						
	$('#form').append(str).show();
	
  layui.use(['form', 'layer'], function(){
		var form = layui.form,
		layer = layui.layer,
		$ = layui.$;
		
		$('#sendEmail').click(function(){
			var title = $('input[name=title]').val(),
					senderName = $('input[name=senderName]').val(),
					senderPhone = $('input[name=senderPhone]').val(),
					senderEmail = $('input[name=senderEmail]').val(),
					content = $('textarea[name=desc]').val();
					
			var obj = {
				"content": content,
				"title": title,
				"senderEmail": senderEmail,
				"senderName": senderName,
				"senderPhone": senderPhone
			};

			if(obj.title != '' && obj.content != '' && obj.senderName != '' && obj.senderPhone != ''){
				saveMail(obj);
			}
		})
		
		//阻止表单提交
		form.on('submit(sendEmail)', function(data){
			return false;
		});
		
	}); 
	
	//监听提交
	function saveMail(obj) {
		$.ajax({
			type:"get",
			url:server+'/expand/saveMail?',
			async:true,
			dataType: "json",
			data: obj,
			success: function(res){
				if(res.resultCode == 0){
					layer.msg('提交成功！');
					console.log(res);
					
					//清空输入框
					$('input[name=title]').val(''),
					$('input[name=senderName]').val(''),
					$('input[name=senderPhone]').val(''),
					$('input[name=senderEmail]').val(''),
					$('textarea[name=desc]').val('');
				}else{
					alert('提交失败')
				}
			},
			error: function(err){
				console.log('请求出错');
			}
		});
	}
	
}



//==========================搜索页面============================

function toSearch(){
	$('#rightContent').empty();
	
	var str = '<div class="download detail-list-right">'+
							'<h1 class="download-title">新闻搜索</h1>'+
							'<div style="width: 500px;margin: 50px auto 100px;">'+
								'<form class="layui-form" lay-filter="boardFilter">'+
									'<div class="layui-form-item" style="margin-bottom: 40px;">'+
										'<label class="layui-form-label">类型</label>'+
										'<div class="layui-input-block">'+
											'<select id="newsType" name="type" lay-verify="required"><option value=""></option></select>'+
										'</div>'+
									'</div>'+
									'<div class="layui-form-item" style="margin-bottom: 40px;">'+
										'<label class="layui-form-label">关键字</label>'+
										'<div class="layui-input-block">'+
											'<input type="text" name="keyword" id="keyword" required  lay-verify="required" placeholder="请输入关键字" autocomplete="off" class="layui-input">'+
										'</div>'+
									'</div>'+
									'<div class="layui-form-item" style="margin-bottom: 40px;">'+
										'<div class="layui-input-block">'+
											'<button class="layui-btn" lay-submit lay-filter="searchNews" id="searchBtn" style="width: 100%;background-color: #1890ff;">搜索</button>'+
										'</div>'+
									'</div>'+
								'</form>'+
							'</div>'+
							'<div class="search-result">搜索结果：<span id="weikong"></span></div>'+
							'<ul class="news-list-box search-list" id="searchList"></ul>'+
						'</div>';
	

	$('#rightContent').append(str);
	$('html,body').scrollTop(0);	
	
 	layui.use('form', function(){
		var form = layui.form;
		
    //加载select数据
		$.ajax({
			type:"get",
			url: server+'/expand/getModularlList',
			async:true,
			dataType: "json",
			success: function(data){
				if(data.resultCode == 0){
					var data = data.resultData;
					for(var i=0; i<data.length; i++){
						var option = $("<option>").val(data[i].core).text(data[i].modelName);
						$('#newsType').append(option);
					}
				}
				form.render();
			},
			error: function(err){
				console.log('请求出错');
			}
		}); 
		
		//监听提交
		$('#searchBtn').click(function(){
			var optionVal = $('#newsType option:selected').val();
			var keyword = $('#keyword').val();
			
			if(optionVal != '' && keyword != ''){
				getSearchResult(optionVal, keyword, 28, 1);
			}
		})
		
		form.on('submit(searchNews)', function(data){
			//layer.msg(JSON.stringify(data.field));
			//getSearchResult(data.field.type, data.field.keyword, 28, 1);
			return false;
		});
	}); 
	
}


//获取搜索结果
function getSearchResult(modularCore, keyword, length, pageNo) {
	$('#searchList').empty();
	var url = server+'/expand/searchModularInfo?keyword='+keyword+'&length='+length+'&modularCore='+modularCore+'&pageNo='+pageNo;
	
	$.ajax({
		type:"get",
		url: url,
		async:true,
		dataType: "json",
		success: function(data){
			if(data.resultCode == 0){
				var data = data.resultData.dataList;
				console.log(data)
				if(data.length){
					$('#weikong').html('');
					for(var i=0; i<data.length; i++){
					  var li = '<li><a onClick="toDetails('+"'"+data[i].value+"'"+')">'+data[i].title+'</a></li>';				
						$('#searchList').append(li);
					}
				}else{
					$('#weikong').html('为空');
					$('#searchList').empty();
				}
				
			}
		},
		error: function(err){
			console.log('请求出错');
		}
	}); 
}



//==========================下载页面============================

function toDownLoad(){
	$('#rightContent').empty();
	getFileList(15, 1);
	var str = '<div class="download detail-list-right">'+
							'<h1 class="download-title">下载专区</h1>'+
							'<ul class="news-list-box download-list" id="downloadList"></ul>'+
						'</div>';
	
	$('#rightContent').append(str);
	$('html,body').scrollTop(0);
}

//获取下载列表
function getFileList(pageSize, pageNo) {
	$.ajax({
		type:"get",
		url: server+'/expand/getDownLoadFileList?length='+pageSize+'&pageNo='+pageNo,
		async:true,
		dataType: "json",
		ContentType: 'text/html',
		success: function(data){
			console.log(data.resultData)
			if(data.resultCode == 0){
				var listData = data.resultData.dataList;
				pageTotal = data.resultData.total;
				for(var i=0; i<listData.length; i++){
					var str = '<li style="padding-left:8px; padding-right:14px;">'+
											'<a href="javascript:;" onClick="downloadFile('+"'"+listData[i].downloadUrl+"'"+')">'+listData[i].filename+'</a>'+
											'<span>'+listData[i].cteatedTime+'</span>'+
										'</li>';
					
					$('#downloadList').append(str);
				}
				
				if(listData.length){
					$("#downloadList").after('<div id="page2" style="text-align: center;margin-top: 32px;"></div>');
					changePage('/expand/getDownLoadFileList', '', pageTotal);
				}
			}
		},
		error: function(err){
			console.log('请求出错');
		}
	});
}

//点击下载文件
function downloadFile(url) {   
	try{ 
		var elemIF = document.createElement("iframe");   
		elemIF.src = url;   
		elemIF.style.display = "none";   
		document.body.appendChild(elemIF);   
	}catch(e){ 
		window.confirm("下载URL出错!");
	} 
} 








